package com.digital.herbarium.service;

import com.digital.herbarium.Entity.PopularImg;
import com.digital.herbarium.Entity.SpecimenUrl;
import com.digital.herbarium.Exception.CustomException;
import com.digital.herbarium.Mapper.PopularMapper;
import com.digital.herbarium.Mapper.SpecimenMapper;
import com.digital.herbarium.Util.TimeUtil;
import com.digital.herbarium.Util.Watermark;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;

@Service
public class UploadService {

    @Value("${static.url}")
    private String IMAGES_DIR;
    @Autowired
    SpecimenMapper specimenMapper;
    @Autowired
    PopularMapper popularMapper;


    public String uploadImage(MultipartFile file, String filename,Integer isWater,Integer type,Integer video) {
        //type  1 标本，2基础库 3 原植物图库，4 图谱库 5 dna 6 关于我们 7 科普宣传
        File imageFile = null;
        String picDicstory = null;
        String fileUrl = File.separator+"image";
        if(type == 4){
            picDicstory = IMAGES_DIR + File.separator + "atlas";
            fileUrl +=  File.separator + "atlas";
            File furl = new File(picDicstory);
            if(!furl.exists()){
                furl.mkdir();
            }
            imageFile = new File(picDicstory + File.separator+ filename);
        }else if(type == 5){
            picDicstory = IMAGES_DIR + File.separator + "dna";
            fileUrl +=  File.separator + "dna";
            File furl = new File(picDicstory);
            if(!furl.exists()){
                furl.mkdir();
            }
            imageFile = new File(picDicstory + File.separator+ filename);
        }else if(type == 6){
            picDicstory = IMAGES_DIR + File.separator + "aboutus";
            fileUrl +=  File.separator + "aboutus";
            File furl = new File(picDicstory);
            if(!furl.exists()){
                furl.mkdir();
            }
            imageFile = new File(picDicstory + File.separator + filename);
        }else if(type == 7){
            picDicstory = IMAGES_DIR + File.separator + "popular" ;
            fileUrl +=  File.separator + "popular";
            File furl = new File(picDicstory);
            if(!furl.exists()){
                furl.mkdir();
            }
            imageFile = new File(picDicstory +  File.separator+ filename);
        }else{
            picDicstory = IMAGES_DIR;
            File furl = new File(picDicstory);
            if(!furl.exists()){
                furl.mkdir();
            }
            imageFile = new File(IMAGES_DIR + filename);
        }
        try {

            file.transferTo(imageFile);
            //添加水印
            if(isWater == 1){
                Watermark.addWaterMark(picDicstory + File.separator + filename ,  "湖北省药检院数字标本馆");
            }
            //数据入库
            String[] fns = filename.split("\\.");
             fileUrl  += File.separator+ filename;

            SpecimenUrl su = new SpecimenUrl();
            su.setImgUrl(fileUrl);
            if(type == 3){
                su.setPicname(filename);
            }else{
                su.setPicname(fns[0]);
            }

            su.setIsWater(isWater);
            su.setType(type);
            su.setCreatetime(TimeUtil.getNow());
            su.setIsVideo(video);
            specimenMapper.addImg(su);
            return fileUrl;
        } catch (Exception e) {
            // 处理异常
            throw new  CustomException(400,e.getMessage());
        }
    }




    public String uploadImageMove(String fileFullNameCurrent,String fileFullNameTarget,
                                  String filename,Integer isWater,Integer type,Integer video) {
        //type  1 标本，2基础库 3 原植物图库，4 图谱库 5 dna 6 关于我们 7 科普宣传
        File imageFile = null;
        String picDicstory = null;
        String fileUrl = File.separator+"image";
        if(type == 4){
            picDicstory = IMAGES_DIR + File.separator + "atlas";
            fileUrl +=  File.separator + "atlas";
            File furl = new File(picDicstory);
            if(!furl.exists()){
                furl.mkdir();
            }
            imageFile = new File(picDicstory + File.separator+ filename);
        }else if(type == 5){
            picDicstory = IMAGES_DIR + File.separator + "dna";
            fileUrl +=  File.separator + "dna";
            File furl = new File(picDicstory);
            if(!furl.exists()){
                furl.mkdir();
            }
            imageFile = new File(picDicstory + File.separator+ filename);
        }else if(type == 6){
            picDicstory = IMAGES_DIR + File.separator + "aboutus";
            fileUrl +=  File.separator + "aboutus";
            File furl = new File(picDicstory);
            if(!furl.exists()){
                furl.mkdir();
            }
            imageFile = new File(picDicstory + File.separator + filename);
        }else if(type == 7){
            picDicstory = IMAGES_DIR + File.separator + "popular" ;
            fileUrl +=  File.separator + "popular";
            File furl = new File(picDicstory);
            if(!furl.exists()){
                furl.mkdir();
            }
            imageFile = new File(picDicstory +  File.separator+ filename);
        }else{
            picDicstory = IMAGES_DIR;
            File furl = new File(picDicstory);
            if(!furl.exists()){
                furl.mkdir();
            }
            imageFile = new File(IMAGES_DIR + filename);
        }
        try {
           // System.out.println("fileFullNameCurrent"+fileFullNameCurrent);
//            File oldName = new File(fileFullNameCurrent);
      //      System.out.println("fileFullNameCurrent"+fileFullNameTarget+ File.separator + filename);
//            File newName = new File(fileFullNameTarget);
            Files.move(Paths.get(fileFullNameCurrent), Paths.get(fileFullNameTarget+ File.separator + filename));
            //FileUtils.copyDirectory(new File(fileFullNameCurrent), new File(fileFullNameTarget ));
            //添加水印
            if(isWater == 1){
                Watermark.addWaterMark(picDicstory + File.separator + filename ,  "湖北省药检院数字标本馆");
            }
            //数据入库
            String[] fns = filename.split("\\.");
            fileUrl  += File.separator+ filename;

            SpecimenUrl su = new SpecimenUrl();
            su.setImgUrl(fileUrl);
            if(type == 3){
                su.setPicname(filename);
            }else{
                su.setPicname(fns[0]);
            }

            su.setIsWater(isWater);
            su.setType(type);
            su.setCreatetime(TimeUtil.getNow());
            su.setIsVideo(video);
            specimenMapper.addImg(su);
            return fileUrl;
        } catch (Exception e) {
            // 处理异常
            throw new  CustomException(400,e.getMessage());
        }
    }




    public String uploadPopularImage(MultipartFile file, String filename,Integer id,Integer type) {
        //type  1 封面 2 用户头像
        String picDicstory = IMAGES_DIR + File.separator + "popular" ;
        String fileUrl = File.separator+"image" + File.separator + "popular" ;
        if(type == 1){
            fileUrl +=  File.separator + "cover";
            picDicstory += File.separator + "cover";
            File furl = new File(picDicstory);
            if(!furl.exists()) {
                furl.mkdir();
            }
        }else if(type == 2){
            fileUrl +=  File.separator + "userImg";
            picDicstory += File.separator + "userImg";
            File furl = new File(picDicstory) ;
            if(!furl.exists()){
                furl.mkdir();
            }
        }
        try {
            File imageFile = new File(picDicstory +  File.separator+ filename);
            file.transferTo(imageFile);
            //数据入库
            String[] fns = filename.split("\\.");
            fileUrl  += File.separator+ filename;

            PopularImg popularImg = new PopularImg();
            popularImg.setPid(id);
            popularImg.setType(type);
            popularImg.setImgUrl(fileUrl);
            popularMapper.addPopularImg(popularImg);
            return fileUrl;
        } catch (Exception e) {
            // 处理异常
            throw new  CustomException(400,e.getMessage());
        }
    }
}